<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>链接二维码生成工具</title>
    <script src="../js/qrcode.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #ffffff;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
        }

        .input-buttons {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        input[type="text"] {
            width: 300px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 1em;
        }

        button {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            font-size: 1em;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        #generateBtn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        #generateBtn:hover {
            background: linear-gradient(135deg, #526ac5 0%, #623c86 100%);
        }

        #cancelBtn {
            background-color: #999;
            color: white;
        }

        #cancelBtn:hover {
            background-color: #777;
        }

        #qrcode {
            margin-top: 30px;
            padding: 20px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        #qrcode img {
            width: 350px;
            height: 350px;
            object-fit: contain;
        }

        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.4);
        }

        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 300px;
            border-radius: 10px;
            text-align: center;
            box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
        }

        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div class="input-buttons">
        <input type="text" id="qrText" placeholder="请输入链接地址">
        <button id="generateBtn" onclick="generateQRCode()">生成二维码</button>
        <button id="cancelBtn" onclick="cancelQRCode()">取消</button>
    </div>
    <div id="qrcode"></div>

    <!-- 模态框 -->
    <div id="myModal" class="modal">
        <div class="modal-content">
            <span class="close">&times;</span>
            <p>请输入链接地址！</p>
        </div>
    </div>

    <script>
        const input = document.getElementById('qrText');
        input.addEventListener('input', function (e) {
            this.value = this.value.replace(/[^a-zA-Z0-9:/.?&=_-]/g, '');
        });

        function generateQRCode() {
            const text = document.getElementById('qrText').value;
            if (text.trim() === '') {
                const modal = document.getElementById('myModal');
                modal.style.display = 'block';
                const span = document.getElementsByClassName("close")[0];
                span.onclick = function () {
                    modal.style.display = 'none';
                }
                window.onclick = function (event) {
                    if (event.target == modal) {
                        modal.style.display = 'none';
                    }
                }
            } else {
                const qr = qrcode(4, 'L');
                qr.addData(text, 'Byte');
                qr.make();
                const qrDiv = document.getElementById('qrcode');
                qrDiv.innerHTML = qr.createImgTag(4);
            }
        }

        function cancelQRCode() {
            document.getElementById('qrText').value = '';
            document.getElementById('qrcode').innerHTML = '';
        }
    </script>
</body>

</html>
    